package com.example.aggregatedemo.arrary;

import com.example.aggregatedemo.arrary.a06HashMap.Student_TreeMap;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

@SpringBootTest
public class TreeMapTest_1 {

    @Test
    void DemoTest1() {

        //创建对象 排序
        TreeMap<Integer,String> ts = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        ts.put(4,"s4");
        ts.put(5,"s5");
        ts.put(2,"s2");
        ts.put(1,"s1");
        ts.put(3,"s3");
        System.out.println(ts);
    }

    @Test
    void DemoTest2() {
        //按照年龄排序
       Student_TreeMap s1=new Student_TreeMap("zhangsan",23);
       Student_TreeMap s2=new Student_TreeMap("lisi",15);
       Student_TreeMap s3=new Student_TreeMap("zhangsan",15);
       Student_TreeMap s4=new Student_TreeMap("wangwu",213);
       Student_TreeMap s5=new Student_TreeMap("wangwu",50);

       TreeMap<Student_TreeMap,String> tm=new TreeMap<>();
        tm.put(s1,"鞍山");
        tm.put(s2,"中国");
        tm.put(s3,"西安");
        tm.put(s4,"汴梁");
        tm.put(s5,"辽宁");

        System.out.println(tm);
    }

    @Test
    void DemoTest3() {
        //
        String s="sadfjmsldfoejrsddjfqaadf";
        //创建集合
        TreeMap<Character,Integer> tm=new TreeMap<>();

        //遍历字符串得到里面的每一个字符
        for(int i=0;i<s.length();i++){
            char c=s.charAt(i);
            //System.out.println(c);

            if(tm.containsKey(c)){
                int count=tm.get(c);
                count++;
                tm.put(c,count);
            }else{
                tm.put(c,1);
            }
        }

        System.out.println(tm);

        //集合拼接
        StringBuilder sb=new StringBuilder();
//        tm.forEach(new BiConsumer<Character, Integer>() {
//            @Override
//            public void accept(Character key, Integer value) {
//                sb.append(key).append("(").append(value).append(")");
//            }
//        });

        tm.forEach((key,value)->sb.append(key).append("(").append(value).append(")"));
        System.out.println("StringBuilder" + sb);

        StringJoiner sj=new StringJoiner("","","");
        tm.forEach((key,value)->sj.add(key+"").add("(").add(value+"").add(")"));
        System.out.println(sj);
    }

}
